﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;
using GlxRRSDB.Data;
using GlxRRSDB.Facades;
using GlxRRSReporter.DailyReporter;
using GlxRRSMain.Utils;

namespace GlxRRSMain.UserControls
{
    public partial class TicketReportByCinemaControl : UserControl
    {
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        public TicketReportByCinemaControl()
        {
            InitializeComponent();
        }

        private void TicketReportByCinemaControl_Load(object sender, EventArgs e)
        {
            pickerFrom.Value = DateTime.Now; 
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
            cbCinema.ValueMember = "ID";
        }
        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            ReportDataSet.tblCinemaRow cinema = (ReportDataSet.tblCinemaRow)cbCinema.SelectedItem;
            int cinemaid = cinema.ID;
            DateTime date = pickerFrom.Value;
            List<ReportDataSet.tblTicketRevenueRow> listTicketRevenue = TicketReportFacade.getTicketRevenue(date, cinemaid);
            List<ReportDataSet.tblCORevenueRow> listCORevenue = TicketReportFacade.getCORevenue(date, cinemaid);
            //
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailFilmAdmission = new Dictionary<int, Dictionary<DateTime, DailyReportData>>();
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailFilmBO = new Dictionary<int, Dictionary<DateTime, DailyReportData>>();
            //
            TicketReportFacade.getDetailFilmAdmissionWeek(pickerFrom.Value, cinemaid, dictDetailFilmAdmission);
            TicketReportFacade.getDetailFilmBOWeek(pickerFrom.Value, cinemaid, dictDetailFilmBO);
            //
            generateReport(date, cinemaid, cinema.CinemaName, listTicketRevenue, listCORevenue, dictDetailFilmAdmission, dictDetailFilmBO);
        }
        private void generateReport(DateTime date, int cinemaId, string cinemaName,
            List<ReportDataSet.tblTicketRevenueRow> dictTicket,
            List<ReportDataSet.tblCORevenueRow> listCORevenue,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailAdmissionFilm,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailBOFilm)
        {
            TicketCinemaReporter.init(FileHelper.getTicketReportByCinemaTemplate(), FileHelper.genTicketReportByCinemaTemplateOut());
            TicketCinemaReporter.generateTicketReportByCinema(date, cinemaId, cinemaName, dictTicket, listCORevenue,dictDetailAdmissionFilm,dictDetailBOFilm);
        }
    }
}
